// pages/my/friend/index.js
import {
  BASE_URL
} from '../../../constant/index'
import {
  getFriendAllApi,
  getFollowingAllApi,
  getFansAllApi,
  postFollowApi,
  postUnFollowApi
} from '../../../https/user'

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    // 顶部标题栏动态设置
    topBarInfo: {
      navBarHeight: app.globalData.navBarHeight,
      menuRight: app.globalData.menuRight,
      menuTop: app.globalData.menuTop,
      menuHeight: app.globalData.menuHeight,
    },
    current: 1,
    classifyList: ['好友', '关注', '粉丝'],
    friend: {},
    following: {},
    fans: {},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getfriendAll()
    this.getfollowingAll()
    this.getfansAll()
  },
  // 获取好友数据
  async getfriendAll() {
    const getRes = await getFriendAllApi()
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        friend: getRes.data
      })
    }
  },
  // 获取关注数据
  async getfollowingAll() {
    const getRes = await getFollowingAllApi()
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        following: getRes.data
      })
    }
  },
  // 获取粉丝数据
  async getfansAll() {
    const getRes = await getFansAllApi()
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        fans: getRes.data
      })
    }
  },
  // 顶部切换
  async toggleCurrent(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index
    if (index === undefined) {
      index = e.detail.current
    }
    this.setData({
      current: index
    })
  },
  // 切换关注状态
  async toggleFollow(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    let list = this.data.following.list
    console.log(list)
    if (item.isFollowing) {
      const postRes = await postUnFollowApi({
        id: item.id
      })
      wx.showToast({
        title: '已取消',
        icon: 'success'
      })
      const updateList = list.map(i => {
        if (i.id == item.id) {
          return {
            ...i,
            isFollowing: !i.isFollowing
          }; // 修改 status
        } else {
          return i
        }
      })
      console.log(updateList)
      this.setData({
        'following.list': updateList
      })
      console.log(this.data.following)

    } else {
      const postRes = await postFollowApi({
        id: item.id
      })

      if (postRes.code == 200) {
        wx.showToast({
          title: '关注成功',
          icon: 'success'
        })
        const updateList = list.map(i => {
          if (i.id == item.id) {
            return {
              ...i,
              isFollowing: !i.isFollowing
            }; // 修改 status
          } else {
            return i
          }
        })
        console.log(updateList)
        this.setData({
          'following.list': updateList
        })
        console.log(this.data.following)

      }
      if (postRes.statusCode == 403) {
        wx.showToast({
          title: '请先登录后才可关注',
          icon: "none"
        })
        return
      }
    }

  },
  // 返回
  back() {
    wx.navigateBack()
  }
})